(function ($) {
    var ui = {
        $search_text: $('#search-text'),
        $search_keywords: $('#search-keywords'),
        $ui_pager: $('#ui-pager'),
        $btn_prev: $('#btn-prev'),
        $btn_next: $('#btn-next'),
        $banner_item: $('#banner-wrap li'),
        $tab_list: $('#tab-list')
    };
    //轮播组件
    var swiper_plugin = {
        curIndex: 0,
        nlength: ui.$banner_item.length,
        time: 3000,
        init: function () {
            this.view();
            this.listen();
        },
        view: function () {
            var self = this;
        },
        listen: function () {
            var self = this;
            ui.$btn_prev.on('click', function () {
                self.fPrev();
            });
            ui.$btn_next.on('click', function () {
                self.fNext();
            });
            ui.$ui_pager.on('click', 'li', function () {
                self.curIndex = $(this).index();
                self.fToggleActiveByIndex(self.curIndex);
            });
        },
        fPrev: function () { //轮播向前按钮
            var self = this;
            if (0 == self.curIndex) {
                self.curIndex = self.nlength - 1;
            } else {
                self.curIndex -= 1;
            }
            self.fToggleActiveByIndex(self.curIndex);
        },
        fNext: function () { //轮播向后按钮
            var self = this;
            if (self.nlength - 1 == self.curIndex) {
                self.curIndex = 0;
            } else {
                self.curIndex += 1;
            }
            self.fToggleActiveByIndex(self.curIndex);
        },
        fToggleActiveByIndex: function (index) {
            var self = this;
            ui.$banner_item.fadeOut().removeClass('slide-active').eq(index).fadeIn().addClass('slide-active');
            ui.$ui_pager.find('li').removeClass('active').eq(index).addClass('active');
        }
    }
    //初始化轮播插件
    swiper_plugin.init();

    ui.$search_text.focus(function () {
        $(this).closest('form').toggleClass('search-form-active');
        ui.$search_keywords.fadeOut();
    }).blur(function () {
        $(this).closest('form').toggleClass('search-form-active');
        ui.$search_keywords.fadeIn();
    });
})(jQuery, window);
$(function () {
    /*查找菜品*/
    $("#search_dishes").click(function () {
        let $keyword = $("#keyword").val().trim();
        /*匹配汉字*/
        let regExp = /^[\u4E00-\u9FA5\\s]+$/;
        if ($keyword === '') {
            alert("关键词不能为空");
            return;
        }
        if (!regExp.test($keyword)) {
            alert("输入必须全为汉字")
            return;
        }
        $(".showInfo").load("/dishes/selectDishesByName?name=" + $keyword)
    })
    /*查找厨师*/
    $("#search_cook").click(function () {
        let $keyword = $("#keyword").val().trim();
        /*匹配汉字*/
        let regExp = /^[\u4E00-\u9FA5\\s]+$/;
        if ($keyword === '') {
            alert("关键词不能为空");
            return;
        }
        if (!regExp.test($keyword)) {
            alert("输入必须全为汉字")
            return;
        }
        $(".showInfo").load("/user/selectCookByName?name=" + $keyword)
    })
});

/*添加到购物*/
function addToCart(foodid) {
    $.ajax({
        url: "/cart/addToCar",
        dataType: "text",
        type: "get",
        data: {foodid: foodid},
        success: function (res) {
            let $submit = $("#" + foodid);
            if (res === '1') {
                $submit.removeClass("btn-outline-success")
                $submit.addClass("btn-success")
                $submit.text('已加入购物车');
            } else {
                alert("请先登录，然后再加入购物车");
            }
        }
    })
}

/*调用后端传来的api接口*/
function boot(api) {
    /*异步加载到里面*/
    $(".showInfo").load(api);
}

function showDetail(foodid,name) {
    let $detail = $("#detail"+foodid);
    $detail.dialog({
        autoOpen: true,
        modal: true,
        title: name,
        width:1000,
        height:618,
        buttons: [
            {
                text: "加入购物车",
                icon: "ui-icon-cart",
                click: function() {
                    addToCart(foodid);
                    $( this ).dialog( "close" );
                }
            },
            {
                text: "关闭",
                icon: "ui-icon-closethick",
                click: function() {
                    $( this ).dialog( "close" );
                }
            },
        ],
        show: {
            effect: "explode",
            duration: 1000
        },
        hide: {
            effect: "explode",
            duration: 1000
        },
    })
}
